/**
 * Collaborative Story Development Web Application (CSDApp) Copyright Anton
 * Strack 2014
 *
 * This file is part of Collaborative Story Development Application (CSDApp).
 *
 * CSDApp is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * CSDApp is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CSDApp. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package csdwa;

import java.sql.Connection;

/**
 * The user history controller handles the end user's interaction with cRud
 * management of userhistory
 *
 * @author Anton Strack
 */
public class UserHistoryController extends ResourceController {

    private final String VIEW_LIST = "/userhistory/index.jsp";

    public UserHistoryController(Connection conn) {
        super(conn);
    }

    /**
     * This method primarily handles form related requests.
     *
     * @param request the request object
     * @return an updated request object sufficient to complete the request.
     */
    @Override
    public CsdwaRequest handlePostRequest(CsdwaRequest request) {
        /*
         String form = request.getFormId();
         String primaryAction = request.getPrimaryActionTypeName();
         String primaryResource = request.getPrimaryResourceTypeName();
         int id = request.getPrimaryResourceId();
         if (form.isEmpty() == false) {
         if (form.equalsIgnoreCase("delete-notification")) {
         request.removeFormId();
         request = handleDelete(request);
         }
         }
         */
        return request;
    }

    /**
     * This method handles get requests. It accepts the primaryResouce and
     * primaryAction as variables to allow routing from the handleGetRequest
     * method.
     *
     * @param request the request object
     * @return an updated request object sufficient to complete the request.
     */
    @Override
    public CsdwaRequest handleGetRequest(CsdwaRequest request) {
        String primaryAction = request.getPrimaryActionTypeName();
        String primaryResource = request.getPrimaryResourceTypeName();
        String secondaryResource = request.getSecondaryResourceTypeName();
        String secondaryAction = request.getSecondaryActionTypeName();
        String uid = request.getParameter("uid");
        int secondaryId = request.getSecondaryResourceId();
        int id = request.getPrimaryResourceId();
        if ("userhistory".equalsIgnoreCase(primaryResource)) {
            if ("list".equalsIgnoreCase(primaryAction) && uid != null && uid.matches(NUMBER_PATTERN)) {
                request.setView(VIEW_LIST);
                UserHistoryDao uhDao = new UserHistoryDao(conn);
                uhDao.setMarker(QueryMarker.LIMIT, "LIMIT 40");
                uhDao.addUsingQueryMarker(QueryMarker.LIMIT);

                uhDao.setUseRelatedMarkers(true);
                int result;
                result = uhDao.selectUserPostHistories(Integer.parseInt(uid));
                if (result > 0) {
                    request.setBeans(uhDao.getBeans());
                } else {
                    request.setMessage("There are no user histories to display.");
                    request.setMessageType("info");
                }
            } else { // default list shows most history for everyone
                request.setView(VIEW_LIST);
                UserHistoryDao uhDao = new UserHistoryDao(conn);
                uhDao.setMarker(QueryMarker.LIMIT, "LIMIT 40");
                uhDao.addUsingQueryMarker(QueryMarker.LIMIT);

                uhDao.setUseRelatedMarkers(true);
                int result;
                result = uhDao.selectBeans();
                if (result > 0) {
                    request.setBeans(uhDao.getBeans());
                } else {
                    request.setMessage("There are no user histories");
                    request.setMessageType("info");
                }
            }
        }
        return request;
    }
}
